package com.elinkway.infinitemovies.http.parser;

import com.apkfuns.logutils.LogUtils;
import com.elinkway.infinitemovies.bean.InitInfo;

import org.json.JSONObject;

public class InitInfoParser extends LVideoMobileParser<InitInfo> {

    private static final String CITY = "city";
    private static final String CHECK = "check";
    private static final String REPORT = "report";
    private static final String SPLASH = "splash";
    private static final String UPGRADE = "upgrade";
    private static final String UPGRADE_DATA = "upgradeData";
    private static final String VERSION = "version";
    private static final String LINK = "link";
    private static final String TYPE = "type";
    private static final String DOWNLOAD_RATE = "downloadRate";
    private static final String SPLASHINTERVAL = "splashInterval";
    private static final String TAGS = "tags";
    private static final String SPLASHLIMIT = "splashLimit";
    private static final String YIDIANTAG = "yiDianTag";

    @Override
    public InitInfo parse(JSONObject data) throws Exception {
        LogUtils.d("init parser data is " + data);
        InitInfo initInfo = new InitInfo();
        if (data.has(CITY)) {
            String city = data.getString(CITY);
            initInfo.setCity(city);
        }
        if (data.has(CHECK)) {
            boolean check = data.getBoolean(CHECK);
            initInfo.setCheck(check);
        }
        initInfo.setReport(data.optBoolean(REPORT));
        initInfo.setSplash(data.optString(SPLASH));
        if (data.has(UPGRADE)) {
            boolean upgrade = data.getBoolean(UPGRADE);
            initInfo.setUpgrade(upgrade);
            if (upgrade && data.has(UPGRADE_DATA)) {
                JSONObject upgradeData = data.getJSONObject(UPGRADE_DATA);
                if (upgradeData.has(VERSION)) {
                    String version = upgradeData.getString(VERSION);
                    initInfo.setVersion(version);
                }
                if (upgradeData.has(LINK)) {
                    String link = upgradeData.getString(LINK);
                    initInfo.setLink(link);
                }
                if (upgradeData.has(TYPE)) {
                    String type = upgradeData.getString(TYPE);
                    initInfo.setType(type);
                }
            }
        }
        if (data.has(DOWNLOAD_RATE)) {
            int downloadRate = data.getInt(DOWNLOAD_RATE);
            initInfo.setDownloadRate(downloadRate);
        }
        if(data.has(SPLASHINTERVAL)){
            initInfo.setSplashInterval(data.optInt(SPLASHINTERVAL));
        }
        if(data.has(TAGS)){
            initInfo.setTags(data.optString(TAGS));
        }
        if(data.has(SPLASHLIMIT)){
            String gdtSplashLimit = data.optString(SPLASHLIMIT);
            try{
                initInfo.setSplashLimit(Integer.parseInt(gdtSplashLimit));
            }catch (Exception e){

            }
        }
        if(data.has(YIDIANTAG)){
            initInfo.setYiDianTag(data.optString(YIDIANTAG));
        }
        return initInfo;
    }
}
